unit UAufzug;

interface

uses SysUtils, Classes, Math, UMapObjekte, UPictureName;

type
  TAufzug = class(TMapObject) // 9
  private
    MoveUp: boolean;
    MoveCount: integer;

  const
    MAX_MOVE = 150;

  public
    constructor Create(Id: integer); Override;
    function getCollisionAtPixel(Game:TObject;X, Y: integer): integer; Override;
    procedure Action(Game: TObject); Override;

  end;

implementation

uses UGame, UPlayer, UMap;
{ TGumba }

procedure TAufzug.Action(Game: TObject);
var
  Player, Player2: TPlayer;
  Abstand: integer;
  Mitte: integer;
begin
  // Einzelspieler
  if not(TGame(Game).isServer) and not(TGame(Game).isClient) then
  begin
    Player := TGame(Game).ActivePlayer;
    Player2 := nil;
    Abstand := Player.EndeY - self.StartY;
    Mitte := Player.StartX + (Player.Width div 2);

    if not((-2 <= Abstand) and (Abstand <= 2)) then
      Player := Nil
    else if not((self.StartX <= Mitte) and (Mitte <= self.EndeX)) then
      Player := nil;
  end
  else
  begin
    // Multiplayer
    Player := TGame(Game).Player1;
    Abstand := Player.EndeY - self.StartY;
    Mitte := Player.StartX + (Player.Width div 2);

    if not((-2 <= Abstand) and (Abstand <= 2)) then
      Player := Nil
    else if not((self.StartX <= Mitte) and (Mitte <= self.EndeX)) then
      Player := nil;

    Player2 := TGame(Game).Player2;
    Abstand := Player2.EndeY - self.StartY;
    Mitte := Player2.StartX + (Player2.Width div 2);

    if not((-2 <= Abstand) and (Abstand <= 2)) then
      Player2 := Nil
    else if not((self.StartX <= Mitte) and (Mitte <= self.EndeX)) then
      Player2 := nil;
  end;

  if MoveUp then
  begin
    dec(self.FStartY);
    dec(self.FEndeY);
    inc(MoveCount);
    if Player <> nil then
    begin
      Player.StartY := Player.StartY - 1;
      Player.EndeY := Player.EndeY - 1;
    end;

    if Player2 <> nil then
    begin
      Player2.StartY := Player2.StartY - 1;
      Player2.EndeY := Player2.EndeY - 1;
    end;
    if MoveCount = MAX_MOVE then
    begin
      MoveCount := 0;
      MoveUp := False;
    end;
  end
  else
  begin
    inc(self.FStartY);
    inc(self.FEndeY);
    inc(MoveCount);
    if Player <> nil then
    begin
      Player.StartY := Player.StartY + 1;
      Player.EndeY := Player.EndeY + 1;
    end;

    if Player2 <> nil then
    begin
      Player2.StartY := Player2.StartY + 1;
      Player2.EndeY := Player2.EndeY + 1;
    end;
    if MoveCount = MAX_MOVE then
    begin
      MoveCount := 0;
      MoveUp := True;
    end;
  end;

  if Assigned(self.OnMove) then
    self.OnMove(self);
end;

constructor TAufzug.Create(Id: integer);
begin
  self.Id := Id;
  self.Num := 9;
  FPictureName := b_Platform;
  self.affgravity := false;
  MoveUp := True;
  MoveCount := 0;
end;

function TAufzug.getCollisionAtPixel(Game:TObject;X, Y: integer): integer;
begin
  result := 1;
end;

end.
